<template>
  <div class="app-container" tabindex="1" @keyup.enter="handleSearch">
    <el-form
      class="searchform"
      :model="queryParams"
      size="small"
      :inline="true"
    >
      <el-form-item label="编号">
        <el-input
          style="width: 200px"
          v-model="queryParams.agreementNumber"
          placeholder="请输入"
          @keyup.enter.native="handleSearch"
        ></el-input>
      </el-form-item>
      <el-form-item label="客户">
        <OrganizationCompleteSelect
          style="width: 300px"
          v-model="queryParams.organizationName"
          @keyup.enter.native="handleSearch"
        ></OrganizationCompleteSelect>
        <!-- <OrganizationSelect
          style="width: 300px;"
          v-model="queryParams.entrustOrganizationId"
        ></OrganizationSelect> -->
        <!-- <el-input
          v-model="queryParams.organizationName"
          placeholder="请输入"
          @keyup.enter.native="handleSearch"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="项目">
        <ProjectCompleteSelect
          style="width: 300px"
          v-model="queryParams.itemName"
          @keyup.enter.native="handleSearch"
        ></ProjectCompleteSelect>
      </el-form-item>
      <el-form-item label="代理商">
        <el-input
          v-model="queryParams.agent"
          placeholder="请输入"
          @keyup.enter.native="handleSearch"
        ></el-input>
      </el-form-item>
      <el-form-item label="签订日期">
        <el-date-picker
          style="width: 250px"
          v-model="queryParams.signDate"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :unlink-panels="true"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="合同状态">
        <el-select
          style="width: 200px"
          v-model="queryParams.agreementState"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in dict.type.agreement_state"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报告状态">
        <el-select
          style="width: 200px"
          v-model="queryParams.reportState"
          clearable
          filterable
        >
          <el-option label="未生成" :value="0"></el-option>
          <el-option label="已生成" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务员">
        <UserCompleteSelect
          style="width: 200px"
          v-model="queryParams.businessUserName"
          @keyup.enter.native="handleSearch"
        ></UserCompleteSelect>
        <!-- <el-select
          style="width: 200px;"
          v-model="queryParams.businessUser"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in userList"
            :key="index"
            :label="item.nickName"
            :value="item.userId"
          ></el-option>
        </el-select> -->
        <!-- <el-input
          style="width: 200px;"
          v-model="queryParams.businessUserName"
          placeholder="请输入"
          @keyup.enter.native="handleSearch"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="开票状态">
        <el-select
          style="width: 200px"
          v-model="queryParams.invoiceState"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in dict.type.agreement_invoice_state"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="纸质合同是否签订">
        <el-select
          style="width: 200px"
          v-model="queryParams.signStatus"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in dict.type.is_or_no"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注/附言/部门">
        <el-input
          style="width: 200px"
          v-model="queryParams.remarks"
          placeholder="请输入"
          @keyup.enter.native="handleSearch"
        ></el-input>
      </el-form-item>
      <el-form-item label=" " label-width="20px">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-wrapper">
      <div class="tableoperation">
        <el-button
          type="primary"
          v-hasPermi="['basic:entrustAgreement:add']"
          icon="el-icon-plus"
          @click="handleAddContract"
          >新增收入合同</el-button
        >
        <el-button
          type="primary"
          v-hasPermi="['basic:entrustAgreement:import']"
          icon="el-icon-upload2"
          @click="handleImportContract"
          >导入</el-button
        >
        <el-button
          type="primary"
          v-hasPermi="['basic:entrustAgreement:export']"
          icon="el-icon-download"
          @click="handleExportContract"
          >导出</el-button
        >
      </div>

      <el-table
        v-loading="tableLoading"
        :data="tableList"
        height="300px"
        :default-sort="{ prop: 'stateSortOrder', order: 'descending' }"
      >
        <el-table-column
          label="日期/编号"
          align="center"
          prop="agreementNumber"
          min-width="130"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'signDate'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">日期/编号</span>
              <SortIcon
                :prop="'agreementNumber'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.signDate, "{y}-{m}-{d}") }}</span>
            <br />
            <span>{{ scope.row.agreementNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="客户/项目"
          align="center"
          prop=""
          show-overflow-tooltip
          min-width="250"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'organizationName'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">客户/项目</span>
              <SortIcon
                :prop="'itemName'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <span>{{ scope.row.organizationName }}</span>
            <br />
            <span>{{ scope.row.itemName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="代理商"
          align="center"
          prop="agent"
          min-width="120"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span style="line-height: 1">代理商</span>
              <SortIcon
                :prop="'agent'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <span>{{ scope.row.agent }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="业务员/部门"
          align="center"
          prop=""
          min-width="180"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'businessUser'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">业务员/部门</span>
              <SortIcon
                :prop="'deptName'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <span>{{ scope.row.businessUserName }}</span>
            <br />
            <span>{{ scope.row.deptName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="签订金额/实际金额"
          align="center"
          prop=""
          min-width="190"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'signAmount'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">签订金额/实际金额</span>
              <SortIcon
                :prop="'actualAmount'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <span>{{ scope.row.signAmount }}</span>
            <br />
            <span>{{ scope.row.actualAmount }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="已收款/未收款"
          align="center"
          prop=""
          min-width="160"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'paymentAmount'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">已收款/未收款</span>
              <SortIcon
                :prop="'unPaymentAmount'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <div @click="handleCollectMoneyRecord(scope.row)">
              <span>{{ scope.row.paymentAmount }}</span>
              <br />
              <span>{{ scope.row.unPaymentAmount }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="已开票/未开票"
          align="center"
          prop=""
          min-width="160"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'invoiceAmount'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">已开票/未开票</span>
              <SortIcon
                :prop="'unInvoiceAmount'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <div @click="handleInvoicingRecord(scope.row)">
              <span>{{ scope.row.invoiceAmount }}</span>
              <br />
              <span>{{ scope.row.unInvoiceAmount }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="备注/附言"
          align="center"
          prop="remark"
          min-width="140"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <SortIcon
                :prop="'remark'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
              <span style="line-height: 1">备注/附言</span>
              <SortIcon
                :prop="'postscript'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
            <br />
            <span>{{ scope.row.postscript }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="合同状态"
          align="center"
          prop="agreementState"
          min-width="100"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span style="line-height: 1">合同状态</span>
              <SortIcon
                :prop="'stateSortOrder'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.agreement_state"
              :value="scope.row.agreementState"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="报告状态"
          align="center"
          prop="reportState"
          min-width="100"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span style="line-height: 1">报告状态</span>
              <SortIcon
                :prop="'reportState'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="{ row }">
            <!-- {{ row.reportState == 0 ? "未生成" : row.reportState == 1 ? "已生成" : "" }} -->
            <dict-tag
              :options="dict.type.report_state"
              :value="row.reportState"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="开票状态"
          align="center"
          prop="invoiceState"
          min-width="100"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span style="line-height: 1">开票状态</span>
              <SortIcon
                :prop="'invoiceState'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.agreement_invoice_state"
              :value="scope.row.invoiceState"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="纸质合同是否签订"
          align="center"
          prop="signStatus"
          width="100"
        >
          <template slot="header">
            <div
              style="
                display: flex;
                align-items: center;
                justify-content: center;
              "
            >
              <span style="line-height: 1">纸质合同是否签订</span>
              <SortIcon
                :prop="'signStatus'"
                :active-sort="activeSort"
                @sort-change="handleSortChange"
              />
            </div>
          </template>
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.is_or_no"
              :value="scope.row.signStatus"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          prop=""
          fixed="right"
          width="150"
        >
          <template slot-scope="scope">
            <!-- 草稿状态 才能修改 -->
            <el-link
              style="margin-left: 10px"
              type="primary"
              @click="handleEditContract(scope.row)"
              v-if="
                checkPermi(['basic:entrustAgreement:edit']) &&
                scope.row.agreementState == 0 &&
                scope.row.beOperate
              "
              >修改</el-link
            >
            <!-- 草稿状态 才能删除 -->
            <el-link
              style="margin-left: 10px"
              type="danger"
              @click="handleDeleteContract(scope.row)"
              v-if="
                checkPermi(['basic:entrustAgreement:delete']) &&
                scope.row.agreementState != 3 &&
                scope.row.paymentAmount <= 0 &&
                scope.row.progressInvoiceAmount <= 0 &&
                scope.row.invoiceAmount <= 0 &&
                scope.row.beOperate
              "
              >删除</el-link
            >

            <el-dropdown
              size="mini"
              @command="(command) => handleOperateCommand(command, scope.row)"
              trigger="click"
            >
              <el-button size="mini" type="text" icon="el-icon-d-arrow-right"
                >更多</el-button
              >
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="lookContract"
                  >查看合同</el-dropdown-item
                >
                <!-- 未完成状态 才能变更合同 -->
                <el-dropdown-item
                  command="updateContract"
                  v-if="
                    checkPermi(['basic:entrustAgreement:updateSubmit']) &&
                    scope.row.agreementState == 1 &&
                    scope.row.beOperate
                  "
                  >变更合同</el-dropdown-item
                >
                <!-- 未草稿状态 才能变更签订 -->
                <el-dropdown-item
                  command="changeSignStatus"
                  v-if="
                    checkPermi(['basic:entrustAgreement:changeSignStatus']) &&
                    scope.row.agreementState != 0
                  "
                  >变更签订</el-dropdown-item
                >
                <!-- 未完成状态 && 未收款金额≤0 才能完成合同 -->
                <el-dropdown-item
                  command="completeContract"
                  v-if="
                    checkPermi(['basic:entrustAgreement:completed']) &&
                    scope.row.agreementState == 1 &&
                    scope.row.unPaymentAmount <= 0 &&
                    scope.row.beOperate
                  "
                  >完成合同</el-dropdown-item
                >
                <!-- 已完成状态 才能开启合同 -->
                <el-dropdown-item
                  command="openContract"
                  v-if="
                    checkPermi(['basic:entrustAgreement:open']) &&
                    scope.row.agreementState == 3 &&
                    scope.row.beOperate
                  "
                  >开启合同</el-dropdown-item
                >
                <!-- 非草稿状态 才能合同评审 -->
                <el-dropdown-item
                  command="contractReview"
                  v-if="
                    checkPermi(['basic:entrustAgreement:saveContractReview']) &&
                    scope.row.agreementState != 0 &&
                    scope.row.beOperate
                  "
                  >合同评审</el-dropdown-item
                >
                <!-- 非草稿状态 才能技术服务委托协议书 -->
                <el-dropdown-item
                  command="agreementBook"
                  v-if="
                    checkPermi(['basic:entrustAgreement:saveContractReview']) &&
                    scope.row.agreementState != 0 &&
                    scope.row.beOperate
                  "
                  >委托协议书</el-dropdown-item
                >
                <!-- 合同状态未完成 && 未收款金额大于0 才能收款 -->
                <el-dropdown-item
                  command="collectMoney"
                  v-if="
                    checkPermi(['basic:agreementPaymentRecord:add']) &&
                    scope.row.agreementState == '1' &&
                    scope.row.unPaymentAmount &&
                    scope.row.beOperate
                  "
                  >收款</el-dropdown-item
                >
                <!-- 绑定合同评审任务 -->
                <!-- <el-dropdown-item command="bindContract" v-if="checkPermi(['basic:entrustAgreement:bindContractTask']) && scope.row.bindContractTask && scope.row.beOperate">绑定合同评审任务</el-dropdown-item> -->
                <!-- 非草稿状态 才能派发 -->
                <el-dropdown-item
                  command="distribute"
                  :disabled="scope.row.hasTask == 1"
                  v-if="scope.row.agreementState != 0 && scope.row.beOperate"
                >
                  {{ scope.row.hasTask == 1 ? "已派发" : "派发" }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        :total="tableTotal"
        @pagination="getTableListData"
      ></pagination>

      <div
        style="
          margin-top: 10px;
          display: flex;
          align-items: center;
          justify-content: flex-end;
          flex-wrap: wrap;
          color: #555;
          font-size: 15px;
        "
      >
        <span>合同金额统计：{{ tableCountData.agreementAmountTotal }}</span>
        <span style="margin-left: 20px"
          >收款金额统计：{{ tableCountData.paymentAmountTotal }}</span
        >
        <span style="margin-left: 20px"
          >待收余额统计：{{ tableCountData.unPaymentAmountTotal }}</span
        >
        <span style="margin-left: 20px"
          >开票金额统计：{{ tableCountData.invoiceAmountTotal }}</span
        >
        <span style="margin-left: 20px"
          >开票余额统计：{{ tableCountData.unInvoiceAmountTotal }}</span
        >
        <span style="margin-left: 20px"
          >纸质合同已签订合计：{{ tableCountData.signTotal }}</span
        >
        <span style="margin-left: 20px"
          >纸质合同未签订合计：{{ tableCountData.unSignTotal }}</span
        >
      </div>
    </div>

    <!-- 导入收入合同弹框 -->
    <ImportContractDialog
      :show.sync="showImportContractDialog"
      @refresh="getTableListData"
    ></ImportContractDialog>

    <!-- 新增/修改 收入合同弹框 -->
    <AddEditContractDialog
      :show.sync="showAddEditContractDialog"
      :operType="operType"
      :row="currRow"
      @refresh="getTableListData"
    ></AddEditContractDialog>

    <!-- 变更签订状态 弹框 -->
    <ChangeSignStatusDialog
      :show.sync="showChangeSignStatusDialog"
      :row="currRow"
      @refresh="getTableListData"
    ></ChangeSignStatusDialog>

    <!-- 合同平生记录表 弹框 -->
    <ContractReviewRecordTableDialog
      :show.sync="showContractReviewRecordTableDialog"
      :row="currRow"
      :entry="2"
      @refresh="getTableListData"
    ></ContractReviewRecordTableDialog>

    <!-- 技术服务委托协议书 弹框 -->
    <EntrustAgreementBook
      :show.sync="showAgreementBookDialog"
      :agreementId="currRow.agreementId"
    ></EntrustAgreementBook>

    <!-- 绑定合同评审任务 弹框 -->
    <BindContractTaskDialog
      :show.sync="showBindContractTaskDialog"
      :row="currRow"
      @refresh="getTableListData"
    ></BindContractTaskDialog>

    <!-- 完成 收入合同弹框 -->
    <CompleteContractDialog
      :show.sync="showCompleteContractDialog"
      :row="currRow"
      @refresh="getTableListData"
    ></CompleteContractDialog>

    <!-- 派发弹框 -->
    <DistributeContractDialog
      :show.sync="showDistributeDialog"
      :row="currRow"
      @refresh="getTableListData"
    ></DistributeContractDialog>

    <!-- 收款弹框 -->
    <CollectMoneyDialog
      :show.sync="showCollectMoneyDialog"
      :row="currRow"
      @refresh="getTableListData"
    ></CollectMoneyDialog>

    <!-- 收款记录弹框 -->
    <CollectMoneyRecordDialog
      :show.sync="showCollectMoneyRecordDialog"
      :row="{
        agreementId: currRow.agreementId,
      }"
    ></CollectMoneyRecordDialog>

    <!-- 开票记录弹框 -->
    <InvoicingRecordDialog
      :show.sync="showInvoicingRecordDialog"
      :row="{
        agreementId: currRow.agreementId,
      }"
    ></InvoicingRecordDialog>
  </div>
</template>

<script>
import { userListingIgnorePermission } from "@/api/system/user";
import ImportContractDialog from "./components/ImportContractDialog.vue";
import AddEditContractDialog from "./components/AddEditContractDialog.vue";
import ChangeSignStatusDialog from "./components/ChangeSignStatusDialog.vue";
import ContractReviewRecordTableDialog from "@/views/slide/component/ContractReviewRecordTableDialog";
import EntrustAgreementBook from "./components/EntrustAgreementBook.vue";
import BindContractTaskDialog from "./components/BindContractTaskDialog.vue";
import CompleteContractDialog from "./components/CompleteContractDialog.vue";
import DistributeContractDialog from "./components/DistributeContractDialog.vue";
import CollectMoneyDialog from "../collectmoney/components/CollectMoneyDialog.vue";
import CollectMoneyRecordDialog from "../collectmoney/components/CollectMoneyRecordDialog.vue";
import InvoicingRecordDialog from "../invoicing/components/InvoicingRecordDialog.vue";
import {
  contractList,
  contractOpen,
  contractDelete,
  genContractCount,
} from "@/api/person/contract";
import SortIcon from "./components/sortIcon";

export default {
  name: "contract",
  dicts: [
    "agreement_state",
    "agreement_invoice_state",
    "report_state",
    "is_or_no",
  ],
  components: {
    ImportContractDialog,
    AddEditContractDialog,
    ChangeSignStatusDialog,
    ContractReviewRecordTableDialog,
    EntrustAgreementBook,
    BindContractTaskDialog,
    CompleteContractDialog,
    DistributeContractDialog,
    CollectMoneyDialog,
    CollectMoneyRecordDialog,
    InvoicingRecordDialog,
    SortIcon,
  },
  data() {
    return {
      userList: [],
      queryParams: {},
      tableLoading: false,
      tableList: [],
      tableTotal: 0,
      tableCountData: {},

      showImportContractDialog: false,
      showAddEditContractDialog: false,
      showContractReviewRecordTableDialog: false,
      showAgreementBookDialog: false,
      showBindContractTaskDialog: false,
      showChangeSignStatusDialog: false,
      showCompleteContractDialog: false,
      showCollectMoneyDialog: false,
      showCollectMoneyRecordDialog: false,
      showInvoicingRecordDialog: false,
      showDistributeDialog: false,
      currRow: {},
      operType: "add",
      activeSort: "",
    };
  },
  created() {
    this.getUserList();
    this.initQueryParams();
    this.getTableListData();
  },
  methods: {
    // 获取用户列表
    getUserList() {
      userListingIgnorePermission()
        .then((res) => {
          this.userList = res.data || [];
        })
        .catch((err) => {});
    },
    // 初始化查询参数
    initQueryParams() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        agreementNumber: "",
        entrustOrganizationId: "",
        organizationName: "",
        itemName: "",
        agent: "",
        signDate: [],
        agreementState: "",
        reportState: "",
        businessUser: "",
        businessUserName: "",
        invoiceState: "",
        signStatus: "",
        remarks: "",
        orderByColumn: "stateSortOrder",
        isAsc: "ascending",
      };
    },
    // 获取查询参数
    getQueryParams() {
      const params = JSON.parse(JSON.stringify(this.queryParams));
      params.beginTimeStart = params.signDate ? params.signDate[0] : "";
      params.beginTimeEnd = params.signDate ? params.signDate[1] : "";
      delete params.signDate;
      return params;
    },
    // 排序
    handleSortChange({ prop, order }) {
      this.queryParams.orderByColumn = prop;
      this.queryParams.isAsc = order;
      this.getTableListData();
      this.activeSort = `${prop}${order}`;
    },
    // 获取表格数据
    getTableListData() {
      this.tableLoading = true;
      const params = this.getQueryParams();
      contractList(params)
        .then((res) => {
          this.tableLoading = false;
          this.tableList = res.rows || [];
          this.tableTotal = res.total || 0;
        })
        .catch((err) => {
          this.tableLoading = false;
        });
      this.genContractCountData();
    },
    // 获取表格统计数据
    genContractCountData() {
      const params = this.getQueryParams();
      delete params.pageNum;
      delete params.pageSize;
      genContractCount(params)
        .then((res) => {
          this.tableCountData = res.data || {};
          console.log("tableCountData", this.tableCountData);
        })
        .catch((err) => {});
    },
    // 点击搜索
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.getTableListData();
    },
    // 点击重置
    handleReset() {
      this.initQueryParams();
      this.getTableListData();
    },
    // 点击查看收入合同
    handleLookContract(row) {
      this.operType = "detail";
      this.currRow = row;
      this.showAddEditContractDialog = true;
    },
    // 点击新增收入合同
    handleAddContract() {
      this.operType = "add";
      this.showAddEditContractDialog = true;
    },
    // 点击修改收入合同
    handleEditContract(row) {
      this.operType = "edit";
      this.currRow = row;
      this.showAddEditContractDialog = true;
    },
    // 点击变更收入合同
    handleUpdateContract(row) {
      this.operType = "update";
      this.currRow = row;
      this.showAddEditContractDialog = true;
    },
    // 点击变更签订状态
    handleChangeSignStatus(row) {
      this.currRow = row;
      this.showChangeSignStatusDialog = true;
    },
    // 点击合同评审
    handleContractReviewRecordTable(row) {
      this.currRow = row;
      this.showContractReviewRecordTableDialog = true;
    },
    // 点击技术服务委托协议书
    handleAgreementBook(row) {
      this.currRow = row;
      this.showAgreementBookDialog = true;
    },
    // 点击绑定合同评审任务
    handleBindContractTask(row) {
      this.currRow = row;
      this.showBindContractTaskDialog = true;
    },
    // 点击完成收入合同
    handleCompleteContract(row) {
      this.currRow = row;
      this.showCompleteContractDialog = true;
    },
    // 点击开启收入合同
    handleOpenContract(row) {
      this.$modal
        .confirm("是否确认开启该收入合同?")
        .then(() => {
          return contractOpen({ agreementId: row.agreementId });
        })
        .then(() => {
          this.getTableListData();
          this.$modal.msgSuccess("开启成功");
        })
        .catch((err) => {});
    },
    // 点击收款
    handleCollectMoney(row) {
      this.currRow = row;
      this.showCollectMoneyDialog = true;
    },
    // 点击收款记录
    handleCollectMoneyRecord(row) {
      if (!row.paymentAmount || Number(row.paymentAmount) == 0) {
        this.$message.error("该合同暂无收款记录");
        return;
      }
      this.currRow = row;
      this.showCollectMoneyRecordDialog = true;
    },
    // 点击开票记录
    handleInvoicingRecord(row) {
      if (!row.invoiceAmount || Number(row.invoiceAmount) == 0) {
        this.$message.error("该合同暂无开票记录");
        return;
      }
      this.currRow = row;
      this.showInvoicingRecordDialog = true;
    },
    // 点击派发
    handleDistribute(row) {
      this.currRow = row;
      this.showDistributeDialog = true;
    },
    // 点击删除收入合同
    handleDeleteContract(row) {
      this.$modal
        .confirm("是否确认删除该收入合同?")
        .then(() => {
          return contractDelete(row.agreementId);
        })
        .then(() => {
          this.getTableListData();
          this.$modal.msgSuccess("删除成功");
        })
        .catch((err) => {});
    },
    // 点击下拉操作菜单项
    handleOperateCommand(command, row) {
      if (command === "lookContract") {
        this.handleLookContract(row);
      } else if (command === "updateContract") {
        this.handleUpdateContract(row);
      } else if (command === "changeSignStatus") {
        this.handleChangeSignStatus(row);
      } else if (command === "completeContract") {
        this.handleCompleteContract(row);
      } else if (command === "openContract") {
        this.handleOpenContract(row);
      } else if (command === "contractReview") {
        this.handleContractReviewRecordTable(row);
      } else if (command === "agreementBook") {
        this.handleAgreementBook(row);
      } else if (command === "collectMoney") {
        this.handleCollectMoney(row);
      } else if (command === "bindContract") {
        this.handleBindContractTask(row);
      } else if (command === "distribute") {
        this.handleDistribute(row);
      }
    },
    // 点击导入收入合同
    handleImportContract() {
      this.showImportContractDialog = true;
    },
    // 点击导出收入合同
    handleExportContract() {
      const params = this.getQueryParams();
      delete params.pageNum;
      delete params.pageSize;
      this.download(
        "/basic/entrust/agreement/export",
        params,
        `收入合同列表.xlsx`
      );
    },
  },
};
</script>

<style scoped lang="scss">
.app-container {
}
.active-asce-sort {
  border-bottom-color: #2a72ee;
}
</style>
